//
//  JPHomeBottomView.m
//  GC_OC
//
//  Created by Kris on 2023/4/19.
//

#import "JPHomeBottomView.h"

@interface JPHomeBottomView()

@property (nonatomic, strong) NSTimer *timer;

@end

@implementation JPHomeBottomView
{
    XLCircleProgress *_circle;
}

- (void)awakeFromNib
{
    [super awakeFromNib];
    
    [self startAnimating];

    
    self.imgView.layer.borderWidth = 1;
    self.imgView.layer.borderColor = ColorFromHex(0x0091FF).CGColor;
    
    self.circle = [[XLCircleProgress alloc] init];
    [self addSubview:self.circle];
    [self.circle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.statusImg);
        make.width.height.mas_equalTo(31);
    }];
    [self.circle layoutIfNeeded];
    [self.circle initUI];
    self.circle.progress = 0;
    
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(startTimer) userInfo:nil repeats:YES];
//    UIButton *btn = [[UIButton alloc] init];
//    [btn addTarget:self action:@selector(act) forControlEvents:UIControlEventTouchUpInside];
//    [self addSubview:btn];
//    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.center.mas_equalTo(0);
//        make.size.mas_equalTo(CGSizeMake(50, 50));
//    }];
}

//- (void) act {
//    [[NSNotificationCenter defaultCenter] postNotificationName:@"noNext" object:nil];
//}

- (void)startTimer
{
    self.circle.progress = [SUPlayerManager manager].progress;
}

- (IBAction)didClickBG:(UIButton *)sender
{
    if (self.tapBlock) {
        self.tapBlock(sender.tag, self.statusImg);
    }
    
    if (sender.tag == 1) {
        if ([SUPlayerManager manager].status == SUPlayStatusPlay) {
            [[SUPlayerManager manager] pausePlay];
            self.statusImg.image = [UIImage imageNamed:@"narrow_playBtn_puse"];
            
            [self stopAnimating];
            
        } else if ([SUPlayerManager manager].status == SUPlayStatusPause) {
            [[SUPlayerManager manager] startPlay];
            self.statusImg.image = [UIImage imageNamed:@"narrow_playBtn_play"];

            [self startAnimating];
        }
    } else if (sender.tag == 3) {
        
        sender.selected = !sender.selected;
        if (sender.selected) {
            self.listBtn.hidden = YES;
            self.playBtn.hidden = YES;
            self.circle.hidden = YES;
            self.clickBtn.hidden = YES;
            self.statusImg.hidden = YES;
            
            [UIView animateWithDuration:0.5 animations:^{
                self.showBGViewLeft.constant = SCREEN_WIDTH - 20 - 50 - 20;
                UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.showBGView.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerBottomLeft cornerRadii:CGSizeMake(30, 30)];
                CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
                maskLayer.frame = self.showBGView.bounds;
                maskLayer.path = maskPath.CGPath;
                self.showBGView.layer.mask = maskLayer;
                [self layoutIfNeeded];
            } completion:^(BOOL finished) {
                self.classLabel.hidden = YES;
                self.nameLabel.hidden = YES;
            }];
        } else {
            [UIView animateWithDuration:0.2 animations:^{
                self.showBGViewLeft.constant = 0;
                [self layoutIfNeeded];
            } completion:^(BOOL finished) {
                self.clickBtn.hidden = NO;
                self.circle.hidden = NO;
                self.listBtn.hidden = NO;
                self.playBtn.hidden = NO;
                self.statusImg.hidden = NO;
                self.classLabel.hidden = NO;
                self.nameLabel.hidden = NO;
                
                UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.showBGView.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerBottomLeft cornerRadii:CGSizeMake(0, 0)];
                CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
                maskLayer.frame = self.showBGView.bounds;
                maskLayer.path = maskPath.CGPath;
                self.showBGView.layer.mask = maskLayer;
            }];
        }
    }
}


-(void)startAnimating
{
    //先判断是否已设置动画，如果已设置则执行动画
    if([_imgView.layer animationForKey:@"rotatianAnimKey"]){
        //如果动画正在执行则返回，避免重复执行动画
        if (_imgView.layer.speed == 1) {
            //speed == 1表示动画正在执行
            return;
        }
        //让动画执行
        _imgView.layer.speed = 1;
        
        //取消上次设置的时间
        _imgView.layer.beginTime = 0;
        //获取上次动画停留的时刻
        CFTimeInterval pauseTime = _imgView.layer.timeOffset;
            
        //取消上次记录的停留时刻
        _imgView.layer.timeOffset = 0;
        
        //计算暂停的时间，设置相对于父坐标系的开始时间
        _imgView.layer.beginTime = [_imgView.layer convertTime:CACurrentMediaTime() fromLayer:nil] - pauseTime;
        
    }else{//没有设置动画
        
        //添加动画
        [self addAnimation];
    }
}
-(void)addAnimation
{
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];//rotation.z
    //默认是顺时针效果，若将fromValue和toValue的值互换，则为逆时针效果
    animation.toValue =   [NSNumber numberWithFloat: M_PI *2];
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    animation.duration = 10;//执行一周10秒
    animation.autoreverses = NO;
    animation.cumulative = NO;
    animation.removedOnCompletion = NO;
    animation.fillMode = kCAFillModeForwards;
    animation.repeatCount = FLT_MAX; //如果这里想设置成一直自旋转，可以设置为FLT_MAX，
    [_imgView.layer addAnimation:animation forKey:@"rotatianAnimKey"];
    //添加动画之后，再让动画执行，否则可能出现动画不执行的情况
    [self startAnimating];
}

-(void)stopAnimating
{
    //如果动画已经暂停，则返回，避免重复，时间会记录错误，造成动画继续后不能连续。
    if (_imgView.layer.speed == 0) {
        return;
    }
    //将当前动画执行到的时间保存到layer的timeOffet中
   //一定要先获取时间再暂停动画
    CFTimeInterval pausedTime = [_imgView.layer convertTime:CACurrentMediaTime() fromLayer:nil];
    //将动画暂停
    _imgView.layer.speed = 0;
    //记录动画暂停时间
    _imgView.layer.timeOffset = pausedTime;
    
}


@end
